package com.hkbigdata.datagovernance.governance.assess.calc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hkbigdata.datagovernance.governance.assess.Assessor;
import com.hkbigdata.datagovernance.governance.bean.AssessParam;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessDetail;
import com.hkbigdata.datagovernance.meta.bean.TableMetaInfo;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

@Component("NO_PRODUCE")
//长期没有产出的表
public class NoProduceAssessor extends Assessor {
    @Override
    public void assess(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws  Exception {
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        //获取相关设置（即多久天算没有产出的阈值）
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject metricParamsJsonObj = JSON.parseObject(metricParamsJson);
        Integer days = metricParamsJsonObj.getInteger("days");
        //生成最新的评估日期（就是获取当前信息）
        Date assessDate = DateUtils.parseDate(assessParam.getAssessDate(), new String[]{"yyyy-MM-dd"});

        Calendar assessDateC = Calendar.getInstance();
        assessDateC.setTime(assessDate);
        int assessDateInt = assessDateC.get(Calendar.DATE);


        Calendar modifyDateC = Calendar.getInstance();
        System.out.println(tableMetaInfo.getTableLastModifyTime());
        modifyDateC.setTime(tableMetaInfo.getTableLastModifyTime());
        int modifyInt = assessDateC.get(Calendar.DATE);


        //如果差值天数大于 阈值天数
        if(modifyInt-assessDateInt > days  ){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("长期没有写入操作");
        }
    }
}
